package com.optimus.model;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import com.optimus.model.mapped.CustomerBean;
import com.optimus.model.mapped.ProductBean;

/**
 * @author Optimus
 * 
 */
public class ShoppingCartBean implements Serializable
{
	private static final long serialVersionUID = 1L;

	private List<ProductBean> productList;
	private Double totalPrice;
	private Logger logger = Logger.getLogger(ShoppingCartBean.class);

	/**
	 * @return the productsList
	 */
	public List<ProductBean> getProductsList()
	{
		return productList;
	}

	/**
	 * @param productsList
	 *            the productsList to set
	 */
	public void setProductsList(List<ProductBean> productsList)
	{
		this.productList = productsList;
	}

	/**
	 * @return the totalPrice
	 */
	public Double getTotalPrice()
	{
		java.text.DecimalFormat twoDForm = new java.text.DecimalFormat("#.##");
		return Double.valueOf(twoDForm.format(totalPrice));
	}

	/**
	 * @param totalPrice
	 *            the totalPrice to set
	 */
	public void setTotalPrice(Double totalPrice)
	{
		this.totalPrice = totalPrice;
	}

	/**
	 * This method counts quantity of a given product in the current shopping
	 * cart.
	 * 
	 * @param p
	 *            - the product
	 * @return The number of occurrences in the shopping cart.
	 */
	public int countQuantityOfProduct(ProductBean p)
	{
		int count = 0;
		for (ProductBean product : productList)
		{
			if (product.getId() == p.getId())
			{
				count += p.getShoppingCartQuantity();
			}
		}
		return count;
	}

	/**
	 * add product to cart
	 * 
	 * @param p
	 *            - product to be added
	 */
	public void addProductToCart(ProductBean p, int quantity)
	{
		BasicConfigurator.configure();
		logger.info("Product " + p + " has been added to the shopping cart");
		if (productList.contains(p))
		{
			int index = findPositionOfProduct(p);
			quantity += productList.remove(index).getShoppingCartQuantity();
		}
		p.setShoppingCartQuantity(quantity);
		productList.add(p);
		updateTotal();
	}

	/**
	 * This method finds an index position of a product within the product list
	 * 
	 * @param p
	 *            - the product to find
	 * @return int - the index of the product
	 */
	private int findPositionOfProduct(ProductBean p)
	{
		for (int i = 0; i < productList.size(); i++)
		{
			if (productList.get(i).equals(p))
			{
				return i;
			}
		}
		return 0;
	}

	/**
	 * 
	 * @param p
	 *            - product to be removed
	 * 
	 */
	public void removeProductFromCart(ProductBean p)
	{
		logger.info("Product " + p + " has been removed from the shopping cart");
		productList.remove(p);
		updateTotal();
	}

	/**
	 * This method updates the total of this shopping cart. This method is
	 * called within any change to the <code>productList</code>
	 */
	private void updateTotal()
	{
		totalPrice = 0.0;
		logger.info("total is now being updated");
		for (ProductBean product : productList)
		{
			totalPrice += product.getPrice() * product.getShoppingCartQuantity();
		}
		logger.info("total has been updated. new total is: $" + totalPrice);
	}

	@Override
	public String toString()
	{
		return "Shopping Cart Contains: " + productList + ".\nTotal Price is: " + totalPrice;
	}
}
